//
//  YYEStudentDetailViewController.m
//  YuYingEDU
//
//  Created by zl on 2018/3/28.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEStudentDetailViewController.h"
#import "SPPageMenu.h"
#import "YYEExamResultViewController.h"
#import "YYESectionViewController.h"

@interface YYEStudentDetailViewController ()<SPPageMenuDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) UIImageView *backIV;
@property (nonatomic,strong) UIImageView *headImg;
@property (nonatomic,strong) UIButton *sexBtn;
@property (nonatomic,strong) UILabel *degreeLabel;
@property (nonatomic,strong) UILabel *nameLabel;

@property (nonatomic, strong) YYESectionViewController *secVC;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) SPPageMenu *pageMenu;


@end

@implementation YYEStudentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backIV = [[UIImageView alloc]init];
    self.backIV.image = [UIImage imageNamed:@"navi"];
    [self.view addSubview:self.backIV];
    
    self.headImg = [[UIImageView alloc]init];
    [self.view addSubview:self.headImg];
//    self.headImg.backgroundColor = [UIColor colorWithHexString:@"#F1A8A3"];

    
    self.sexBtn = [[UIButton alloc]init];
    self.sexBtn.layer.cornerRadius = 3;
    self.sexBtn.layer.masksToBounds = YES;
    [self.view addSubview:self.sexBtn];
    self.sexBtn.titleLabel.font = SystemFont(12);
    self.sexBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 2);
    self.sexBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);

    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    self.nameLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:self.nameLabel];
    
    self.degreeLabel = [[UILabel alloc] init];
    self.degreeLabel.textColor = [UIColor colorWithHexString:@"#666666"];;
    self.degreeLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.degreeLabel];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];;
    [self.view addSubview:lineView];
    
    [self.backIV makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.view);
        make.size.equalTo(CGSizeMake(kScreenWidth, 80));
    }];
    
    [self.headImg makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(12);
        make.top.equalTo(self.view.top).offset(50);
        make.size.equalTo(CGSizeMake(78, 78));
    }];
    self.headImg.layer.masksToBounds = YES;
    self.headImg.layer.cornerRadius = 78/2.0;
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImg.bottom).offset(10);
        make.centerX.equalTo(self.headImg.centerX);
    }];
    
    [self.sexBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.right).offset(12);
        make.centerY.equalTo(self.nameLabel.centerY);
        make.size.equalTo(CGSizeMake(30, 15));
        
    }];
    
    [self.degreeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sexBtn.right).offset(15);
        make.centerY.equalTo(self.nameLabel.centerY);
    }];
    
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.nameLabel.bottom).offset(17);
        make.size.equalTo(CGSizeMake(kScreenWidth, 10));
    }];
    
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:self.model.img] placeholderImage:nil];
    
    self.nameLabel.text = self.model.name;
    [self.sexBtn setTitle:self.model.age forState:UIControlStateNormal];
    self.degreeLabel.text = self.model.degree;
    
    if ([self.model.sex isEqualToString:@"男"]) {
        [self.sexBtn setImage:[UIImage imageNamed:@"yyjy_95"] forState:UIControlStateNormal];
        
        self.sexBtn.backgroundColor = [UIColor colorWithHexString:@"#97C7FF"];
    }

    else {
        [self.sexBtn setImage:[UIImage imageNamed:@"yyjy_96"] forState:UIControlStateNormal];
        self.sexBtn.backgroundColor = [UIColor colorWithHexString:@"#F1A8A3"];

    }
    
    [self configUI];

}

- (void)configUI {
    
    NSArray *datas = @[@"学习进度",@"考试结果"];

    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake((kScreenWidth-200)/2, 190, 200, 40) trackerStyle:SPPageMenuTrackerStyleLine];
//    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 190, kScreenWidth, 40) trackerStyle:SPPageMenuTrackerStyleLine];
    pageMenu.selectedItemTitleColor = GeneralBlueColor;
    pageMenu.unSelectedItemTitleColor = LineColor;
    pageMenu.dividingLine.backgroundColor = [UIColor whiteColor];
    pageMenu.tracker.image = [UIImage imageNamed:@"bottomLine"];
    pageMenu.needTextColorGradients = NO;
    pageMenu.delegate = self;
    [self.view addSubview:pageMenu];
    self.pageMenu = pageMenu;
    
    
    YYESectionViewController *secVC = [[YYESectionViewController alloc]init];
//    secVC.delegate = self;
    self.secVC = secVC;
    self.secVC.cid = self.cid;
    self.secVC.isBuy = self.isBuy;
    self.secVC.mid2 = self.model.mid;
    self.secVC.mark = 1;
    //    self.secVC.playback = self.playback;
    //    self.secVC.mid = self.mid;
    
    //    YYECommentViewController *comVC = [[YYECommentViewController alloc]init];
    YYEExamResultViewController *vc = [[YYEExamResultViewController alloc]init];
    vc.cid = self.cid;
    vc.sid = self.model.id;

    
    [self addChildViewController:secVC];
    [self addChildViewController:vc];
    
    
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.frame = CGRectMake(0, self.pageMenu.jc_bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.pageMenu.jc_bottom);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    [self.scrollView addSubview:secVC.view];
    secVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.pageMenu.jc_bottom);
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    
    //    for (NSInteger i = 0; i < self.childViewControllers.count; i ++) {
    //
    //        YYEBaseViewController *baseVC = self.childViewControllers[i];
    //        [self.scrollView addSubview:baseVC.view];
    //
    //        baseVC.view.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 190);
    //        self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * i, 0);
    //        self.scrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    //
    //    }
    pageMenu.bridgeScrollView = self.scrollView;
    [pageMenu setItems:datas selectedItemIndex:0];
    
    
}

#pragma mark -- SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
    if (labs(toIndex - fromIndex) >= 2) {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:NO];
    }else {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:YES];
    }
    
    UIViewController *targetViewController = self.childViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * toIndex, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.pageMenu.jc_bottom);
    [self.scrollView addSubview:targetViewController.view];
    
}

#pragma mark -- UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    UIViewController *targetViewController = self.childViewControllers[index];
    if ([targetViewController isViewLoaded]) return;
    
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.pageMenu.jc_bottom);
    [self.scrollView addSubview:targetViewController.view];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
